home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 June: Reference Library / Dev.CD Jun 95 / Dev.CD Jun 95.toast / What's New? / New System Software Extensions / QuickDraw 3D ß / Programming / SampleCode / BoxTex / BoxTexShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-23  |  7.1 KB  |  292 lines  |  [TEXT/MPCC]

  1. //
  2. // This is box, the QuickDraw 3D starter program.  Written for the
  3. // Getting started with QuickDraw 3D Develop article.  This app does not have 
  4. // graceful error handling - it's putpose is to illustrate a very basic QuickDraw 
  5. // 3D program.
  6. //
  7. // Nick Thompson - January 6th 1994
  8. //
  9. // Modification History:
  10. //
  11. //    1/6/95        nick    initial version based on cube
  12. //    12/31/94    nick    cube modifications for QuickDraw 3d sample code
  13. //  
  14.  
  15. // system headers
  16. #include <Desk.h>
  17. #include <Dialogs.h>
  18. #include <DiskInit.h>
  19. #include <Fonts.h>
  20. #include <Menus.h>
  21. #include <PictUtil.h>
  22. #include <QDOffScreen.h>
  23. #include <QuickDraw.h>
  24. #include <SegLoad.h>
  25. #include <StandardFile.h>
  26. #include <TextEdit.h>
  27.  
  28. // for QuickDraw 3D
  29. #include "QD3D.h"
  30. #include "QD3DMath.h"
  31. #include "QD3DDrawContext.h"
  32. #include "QD3DShader.h"
  33. #include "QD3DTransform.h"
  34. #include "QD3DGroup.h"
  35.  
  36.  
  37. #include "BoxTexShell.h"
  38. #include "BoxTex3DSupport.h"
  39. #include "Textures.h"
  40.  
  41. //-------------------------------------------------------------------------------------------
  42. // function prototypes
  43.  
  44. static void         InitToolbox( void ) ;
  45. static void         MainEventLoop( void ) ;
  46. static void         HandleKeyPress(EventRecord *event) ;
  47. static void         HandleOSEvent(EventRecord *event) ;
  48. void InitDocumentData( DocumentPtr theDocument ) ;
  49. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  50. void DisposeDocumentData( DocumentPtr theDocument) ;
  51.  
  52.  
  53. //-------------------------------------------------------------------------------------------
  54. //
  55.  
  56. Boolean         gQuitFlag         = false ;
  57. WindowPtr        gMainWindow        = nil ;
  58. DocumentRec        gDocument ;
  59.  
  60. //-------------------------------------------------------------------------------------------
  61. // main()
  62. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  63. // and enter the main event loop.  On exit from the main event loop, we want to call
  64. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  65.  
  66. void main(void)
  67. {
  68.     TQ3Status    myStatus;
  69.     Rect        rBounds = { 50, 50, 450, 450 } ;
  70.     Str255        title = "\pSpinning Box" ;
  71.  
  72.     InitToolbox() ;
  73.     
  74.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  75.     myStatus = Q3Initialize();
  76.     if ( myStatus == kQ3Failure )
  77.         DebugStr("\pErInitialize returned failure.");            
  78.  
  79.     // set up our globals
  80.     gQuitFlag = false ;
  81.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,nil); ;
  82.  
  83.     InitDocumentData( &gDocument ) ;
  84.     
  85.     MainEventLoop();
  86.     
  87.     DisposeDocumentData( &gDocument ) ;
  88.     
  89.     //    Close our connection to the QuickDraw 3D library
  90.     myStatus = Q3Exit();
  91.     if ( myStatus == kQ3Failure )
  92.         DebugStr("\pErExit returned failure.");
  93.     
  94. }
  95.  
  96. //-------------------------------------------------------------------------------------------
  97. //
  98.  
  99. void InitDocumentData( DocumentPtr theDocument ) 
  100. {
  101.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  102.     
  103.     theDocument->fGroupScale = 1;                
  104.     theDocument->fGroupCenter = myOrigin ;            
  105.  
  106.     // sets up the 3d data for the scene
  107.     // Create view for QuickDraw 3D.
  108.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  109.  
  110.     // the main display group:
  111.     theDocument->fModel = MyNewModel() ;
  112.     
  113.     // the drawing styles:
  114.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  115.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  116.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  117.  
  118.     // set the rotation matrix the identity matrix
  119.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);
  120.  
  121.     AdjustCamera(    &gDocument, 
  122.                     (gMainWindow->portRect.right - gMainWindow->portRect.left),
  123.                     (gMainWindow->portRect.bottom - gMainWindow->portRect.top) ) ;
  124.                     
  125. }
  126.  
  127. void DisposeDocumentData( DocumentPtr theDocument)
  128. {
  129.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  130.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  131.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  132.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  133.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  134.  
  135. }
  136. //-----------------------------------------------------------------------------
  137. // 
  138.  
  139. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  140. {    
  141.     Q3View_StartRendering(theDocument->fView );
  142.     do {
  143.         SubmitScene( theDocument ) ;
  144.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  145.     return kQ3Success ;
  146. }
  147.  
  148. //-------------------------------------------------------------------------------------------
  149. //
  150.  
  151. short HiWrd(long aLong)
  152. {
  153.     return    (((aLong) >> 16) & 0xFFFF) ;
  154. }
  155.  
  156. //-------------------------------------------------------------------------------------------
  157. //
  158.  
  159. short LoWrd(long aLong)
  160. {
  161.     return    ((aLong) & 0xFFFF) ;
  162.  
  163. }
  164.  
  165. //-------------------------------------------------------------------------------------------
  166. //
  167.  
  168. void InitToolbox()
  169. {
  170.     Handle        menuBar = nil;
  171.  
  172.     MaxApplZone() ;
  173.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  174.     
  175.     InitGraf( &qd.thePort );
  176.     InitFonts();
  177.     InitWindows();
  178.     InitCursor();
  179.  
  180.     FlushEvents( everyEvent, 0 ) ;
  181.     // initialize application globals
  182.     
  183.     gQuitFlag = false;
  184.     
  185. }
  186.  
  187.  
  188. //-------------------------------------------------------------------------------------------
  189. //
  190. void MainEventLoop()
  191. {
  192.     EventRecord     event;
  193.     WindowPtr       window;
  194.     short           thePart;
  195.     Rect            screenRect, updateRect;
  196.     Point            aPoint = {100, 100};
  197.     CGrafPtr        savedPort ;
  198.     
  199.  
  200.     while( !gQuitFlag )
  201.     {
  202.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  203.         {
  204.  
  205.             switch (event.what) {
  206.                 case mouseDown:
  207.                 
  208.                     thePart = FindWindow( event.where, &window );
  209.                     
  210.                     switch( thePart ) {
  211.                         case inMenuBar: 
  212.                             break;
  213.                         
  214.                         case inDrag:
  215.                     
  216.                             screenRect = (**GetGrayRgn()).rgnBBox;
  217.                             DragWindow( window, event.where, &screenRect );
  218.                             break ;
  219.                     
  220.                         case inContent:
  221.                     
  222.                             if (window != FrontWindow())
  223.                                 SelectWindow( window );
  224.                             break ;
  225.                     
  226.                         case inGoAway:
  227.                             if (TrackGoAway( window, event.where )) {
  228.                                 DisposeWindow ( window );
  229.                                 gQuitFlag = true;
  230.  
  231.                             }
  232.                             break ;
  233.                             
  234.                         default:
  235.                             break ;
  236.                     }
  237.                     break ;
  238.                             
  239.                         
  240.                 case updateEvt:
  241.                 
  242.                     window = (WindowPtr)event.message;
  243.                     updateRect = (**(window->visRgn)).rgnBBox;
  244.                     SetPort( window ) ;
  245.                     BeginUpdate( window );
  246.                     DocumentDraw3DData( &gDocument ) ;
  247.                     EndUpdate( window );
  248.                     break ;
  249.                     
  250.                 case keyDown:
  251.                 case autoKey:
  252.                     HandleKeyPress(&event);
  253.                     break;
  254.                     
  255.                 case diskEvt:
  256.                     if ( HiWrd(event.message) != noErr ) 
  257.                         (void) DIBadMount(aPoint, event.message);
  258.                     break;
  259.                     
  260.                 case osEvt:
  261.                 case activateEvt:
  262.                     break;
  263.  
  264.  
  265.             }
  266.         }
  267.         else {
  268.             // we received a null event, rotate the cube
  269.             TQ3Matrix4x4    tmp;
  270.             Rect        theRect = ((GrafPtr)gMainWindow)->portRect ;
  271.             
  272.             SetPort((GrafPtr)gMainWindow) ;
  273.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  274.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  275.  
  276.             InvalRect( &theRect ) ;
  277.         }
  278.     }
  279. }
  280.  
  281.  
  282. //-------------------------------------------------------------------------------------------
  283. //
  284. void HandleKeyPress(EventRecord *event)
  285. {}
  286.  
  287. //-------------------------------------------------------------------------------------------
  288. //
  289.  
  290.  
  291.  
  292.